import { Component, OnInit } from '@angular/core';
import { FormBuilder,FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  loginForm:FormGroup;
  constructor(private fb:FormBuilder) { }

  ngOnInit() {
    this.loginForm = this.fb.group({
      email: ['gjj_kruup@163.com',Validators.compose([Validators.required,Validators.email,this.validate])],
      password: ['',Validators.required]
    })
    // new FormGroup({
    //   email: new FormControl('gjj_kruup@163.com',Validators.compose([Validators.required,Validators.email])),
    //   password: new FormControl('',Validators.required)
    // })
  }
  onSubmit({value,valid},ev){
    ev.preventDefault();
    console.log(JSON.stringify(value));
    console.log(JSON.stringify(valid))
    // 动态指定 验证器
    //this.loginForm.controls['email'].setValidators(this.validate)
  }

  // {[key:string]:any}  返回的对象
  validate(c:FormControl):{[key:string]:any}{
    // 如果是空
    if(!c.value){
      return
    }
    const pattenr =/^wang*/;
    if(pattenr.test(c.value)){
      return null;
    }
    return {
      emalNotValid:'The email must start with wang'
    }

  }
}
